package com.material.controller;

import com.material.common.annotation.LoginUser;
import com.material.common.base.CommonController;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.common.utils.StringUtils;
import com.material.factory.domain.FactoryInfo;
import com.material.factory.service.IFactoryInfoService;
import com.material.supplier.domain.SupplierInfo;
import com.material.supplier.service.ISupplierInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * @author ganxinge
 */
@Api(tags = "10.工厂banner")
@RestController
@RequestMapping("/banner")
public class FactoryBannerController extends CommonController {

    @Autowired
    private IFactoryInfoService factoryInfoService;
    @Autowired
    private ISupplierInfoService supplierInfoService;

    @ApiOperation("首页工厂banner")
    @GetMapping("/list")
    public ResultResponse list(@ApiIgnore @LoginUser SysUser sysUser){
        Long factoryId = 0L;
        String userType = sysUser.getUserType();
        if (SysUser.USER_TYPE_3.equals(userType)){
            Long supplierId = sysUser.getBizId();
            SupplierInfo supplierInfo = supplierInfoService.selectSupplierInfoById(supplierId);
            factoryId = supplierInfo.getFactoryId();
        }
        if(SysUser.USER_TYPE_1.equals(userType) || SysUser.USER_TYPE_2.equals(userType)){
            factoryId = sysUser.getBizId();
        }

        FactoryInfo factoryInfo = factoryInfoService.selectFactoryInfoById(factoryId);
        String banner = factoryInfo.getBanner();
        if(StringUtils.isBlank(banner)){
            return ResultResponse.success();
        }
        String[] split = banner.split(",");
        return ResultResponse.success(Arrays.asList(split));
    }
}
